﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DataBase;

public class AttackControl : MonoBehaviour
{
    //获取当前形态，根据当前形态赋予所有技能ID

    private int currentAttackID;
    private int currentAbilityID;
    private int currentUltimateID;

    private bool canAttack;
    private bool canAbility;
    private bool canUltimate;
    private AttackInput attackInput;

    private PlayerController2D playerControl;
    private PlatformerMotor2D playerMotor;

    private void Awake()
    {
        attackInput = GameObject.Find("AttackInput").gameObject.GetComponent<AttackInput>();
        canAttack = true;
        canAbility = true;
        canUltimate = true;

        playerControl= GameObject.Find("Player").GetComponent<PlayerController2D>();
        playerMotor = GameObject.Find("Player").GetComponent<PlatformerMotor2D>();
        //测试用
        currentAttackID = 1;
    }
	
	void Update ()
    {
     
		if(attackInput.Attack && canAttack)
        {
            StartCoroutine(AttackCondition(currentAttackID));
        }
	}

    IEnumerator AttackCondition(int currentAttackID)
    {
        Debug.Log("开始攻击协程");
        canAttack = false;
        Skill currentSkill = Skill.get(currentAttackID);
        float tempCostTime = 0;


        //通过技能ID读表获得攻击动画、动画时间、造成伤害、攻击范围、攻击距离
        float coldDownTime;
        int animationNum;
        string currentAnimationPath;
        float currentAnimationTime = 1.0f;
        int currentDamageType;
        int currentBuff;
        int currentDebuff;
        bool currentCanMove;
        int currentDisPlacementNum;
        int currentEvent;
        float currentDamage;
        float currentDistance;
        float currentAngle;

        coldDownTime = currentSkill.ColdDown;
        animationNum = currentSkill.AnimationNum;
        for(int currentCount=0; currentCount < animationNum; currentCount++)
        {
            currentAnimationPath = currentSkill.Animation[currentCount];
            currentAnimationTime = currentSkill.AnimationTime[currentCount];
            currentDamageType = currentSkill.DamageType[currentCount];
            currentBuff = currentSkill.Buff[currentCount];
            currentDebuff = currentSkill.Debuff[currentCount];
            currentCanMove = currentSkill.CanMove[currentCount];
            currentDisPlacementNum = currentSkill.DisPlacement[currentCount];
            currentDamage = currentSkill.Damage[currentCount];
            currentAngle = currentSkill.DamageAngle[currentCount];
            currentDistance = currentSkill.DamageDistance[currentCount];
            currentEvent = currentSkill.Event[currentCount];
            //Debug.Log("攻击角度为"+currentAngle);
            //Debug.Log("攻击距离为"+currentDistance);

            //判断动画播放时是否可以移动
            if(currentCanMove)
            {
                playerControl.enabled = false;
                playerMotor.frozen = true;
            }

            //播放攻击动画
            currentAnimationPath = "Prefabs/Attack/" + currentAnimationPath;

            //进行实际攻击和伤害判定
            EventCenter.Broadcast<string, float, float, float,float>(MyEventType.Attack, currentAnimationPath, currentAnimationTime, currentDistance, currentAngle,currentDamage);
            yield return new WaitForSeconds(currentAnimationTime);
            tempCostTime += currentAnimationTime;

            //如果之前禁止移动，现在恢复
            if((!playerControl.enabled)&&(playerMotor.frozen))
            {
                playerControl.enabled = true;
                playerMotor.frozen = false;
            }

        }
        if(coldDownTime>tempCostTime)
        {
            yield return new WaitForSeconds(coldDownTime - tempCostTime);
        }

        canAttack = true;
    }

    /// <summary>
    /// 切换人物时初始化数值，应该读表然后按照身上的装备或其他情况来得到最终数值
    /// </summary>
    private void Init()
    {
        
    }


}
